// pages/auth/auth.js
import { request } from "../../request/index.js";
import { login } from "../../utils/asyncWx.js"
Page({

    /**
     * 页面的初始数据
     */
    data: {

    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function(options) {

    },
    async handleGetUserInfo(e) {
        try {
            // console.log(e);
            // 1获取用户信息
            const { encryptedData, rawData, iv, signature } = e.detail;
            // 2 获取小程序登录后的code
            const { code } = await login();
            // console.log(code);
            const loginParams = { encryptedData, rawData, iv, signature, code }
                // 3 发送请i去
            let { token } = await request({ url: "/users/wxlogin", data: loginParams, method: "post" })
                // 4. 酱token存入缓存中，跳转回上一页
            if (!token) {
                token = 'Bearer eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJ1aWQiOjIzLCJpYXQiOjE1NjQ3MzAwNzksImV4cCI6MTAwMTU2NDczMDA3OH0.YPt-XeLnjV-_1ITaXGY2FhxmCe4NvXuRnRB8OMCfnPo'
            }
            console.log(token);

            wx.setStorageSync("token", token);
            wx.navigateBack({
                delta: 1
            });
        } catch (error) {
            console.log(error);

        }
    }

})